package com.yidiandao.business.ui.main.shoppingcart.chooseredpacket;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.ui.adapter.ChooseRedPacketAdapter;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseRedPacketListItem;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 选择红包页面 View
 */
public class ChooseRedPacketFragment extends SuperBaseFragment implements ChooseRedPacketContract.View {

    private ChooseRedPacketContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private ListView mListView;

    private Button mBtnConfirm;

    private ChooseRedPacketAdapter mAdapter;

    private TextView mTvUseRedPacket;

    private TextView mTvUnUseRedPacket;

    public static ChooseRedPacketFragment getInstance(Bundle extras) {
        ChooseRedPacketFragment fragment = new ChooseRedPacketFragment();
        fragment.setArguments(extras);
        return fragment;
    }

    @Override
    public void setPresenter(ChooseRedPacketContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ChooseRedPacketFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mListView = (ListView) mRootView.findViewById(R.id.list_view);
        mBtnConfirm = (Button) mRootView.findViewById(R.id.btn_confirm);
        mTvUseRedPacket = (TextView) mRootView.findViewById(R.id.tv_use_red_packet);
        mTvUnUseRedPacket = (TextView) mRootView.findViewById(R.id.tv_un_use_red_packet);

        mAdapter = new ChooseRedPacketAdapter(mActivity);
        mListView.setAdapter(mAdapter);

        initListeners();
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPresenter.onItemClick((ChooseRedPacketListItem) parent.getItemAtPosition(position));
            }
        });

        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.onConfirmChoose();
            }
        });

        // 使用红包
        mTvUseRedPacket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshUseRedPacketState(true);
                setListViewVisibility(true);
                mPresenter.setUseRedPacket(true);
            }
        });

        // 不使用红包
        mTvUnUseRedPacket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshUseRedPacketState(false);
                setListViewVisibility(false);
                mPresenter.setUseRedPacket(false);
            }
        });
    }

    @Override
    public void setListViewVisibility(boolean visible) {
        if (visible && mListView.getVisibility() != View.VISIBLE) {
            mListView.setVisibility(View.VISIBLE);
        } else if (!visible && mListView.getVisibility() != View.GONE) {
            mListView.setVisibility(View.GONE);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_red_packet;
    }

    @Override
    public void setDisplayListItems(List<DisplayListItem> displayListItems) {
        mAdapter.setData(displayListItems);
    }

    @Override
    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void finishActivity() {
        mActivity.finish();
    }

    @Override
    public void refreshUseRedPacketState(boolean useRedPacket) {
        mTvUnUseRedPacket.setSelected(!useRedPacket);
        mTvUseRedPacket.setSelected(useRedPacket);
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }
}
